/*
 * Calendar service object based on com.nokia.device.calendar Interface
 */
var calendar = null;

/*
 * Initializes Calendar service object
 */
function initService(){
    try {
        calendar = nokia.device.load("calendar");
    } 
    catch (e) {
        alert(e.toString());
    }
}

/*
 * Gets a list of calendar entries matching provided constrains
 * 
 * type - entry type
 * startDate - entry start date
 * endDate - entry end date
 */
function listEntries(type, startDate, endDate){
    var match = new Object();
    var range = new Object();
    if (type != null && type != "") 
        match.type = type;
    try {
        if (startDate != null && startDate != "") 
            range.begin = parseDate(startDate);
        if (endDate != null && endDate != "") 
            range.end = parseDate(endDate);
        match.range = range;
        calendar.getList(onReceive, match, onReceiveError);
    } 
    catch (e) {
        alert(e);
    }
}


/*
 *  Adds a new calendar entry 
 *  
 *  type - entry type: "Meeting", "ToDo", "Anniversary", "DayEvent" 
 *  NOTE: "Reminder" is not implemented
 *  start - entry start date
 *  end - entry end date
 *  alarm - entry alarm date
 *  summary - entry summary
 *  location - field is only applicable to "Meeting" entries
 *  
 */
function addEntry(type, start, end, alarm, summary, location){
  //  alert(type + "\n"+start + "\n"+end + "\n"+alarm + "\n"+summary + "\n"+location);
    try {
        var time = new Object();
        if (start!=null) 
            time.begin = start;
        if (end!=null) 
            time.end = end;
        if (alarm!=null) 
            time.alarm = alarm;
        
        var entry = new Object();
        entry.type = type;
        entry.summary = summary;
        entry.time = time;
        if (location!=null) 
            entry.location = location;
        
        calendar.addEntry(entry);
    } 
    catch (e) {
        alert(e.toString());
    }
}

/*
 * Starts the device native calendar editor UI
 */
function startEditor(){
    try {
    	calendar.startEditor(onStart);
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}
/*
 * Success Callback function for startEditor
 * Only data is returned
 */
function onStart(entryData){
 	//Do something
}

/*
 * getList Success callback function that handles calendar entries iterator and puts content to entryList container
 * Only data is returned
 */
function onReceive(entryData){
    var entry;
    var content = "<table border='0' width='100%'><tr><th colspan='2'><input type='checkbox' name='chAll' onClick='chALL(this.checked)'/></th><th><ul class='button-rounded'><li><a href='#' onclick='deleteEvents();'>Delete selected</a></li></th></tr>";
    while ((entry = entryData.next()) != null) 
        content += showEntry(entry);
    content += "</table>";
    document.getElementById("entryList").innerHTML = content;
}

/*
 * getList Failure callback function that handles error
 * Only error is returned
 */
function onReceiveError(err){
    alert(err);
}

/*
 * Deletes calendar event by specific id
 * id - unique id of event to be deleted
 */
function deleteEvent(entryid){
    try {
        var data = {
            id: entryid
        }; 
        calendar.deleteEntry(data);
    } 
    catch (e) {
        alert(e.toString());
    }
}